package com.sbox;

import com.sbox.model.SBoxConfig;
import com.sbox.util.Utils;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.Toast;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;

public class SettingActivity extends Activity {
	
	private OnCheckedChangeListener mCheckedListener = new OnCheckedChangeListener()
	{
		@Override
		public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
		{
			if( buttonView != mCheckPassword )
				return;
			
			if( isChecked )
			{				
				mBtnCancel.setEnabled(true);
				mEditPassword1.setEnabled(true);
				mEditPassword2.setEnabled(true);
			}
			else
			{				
				mBtnCancel.setEnabled(false);				
				mEditPassword1.setEnabled(false);
				mEditPassword2.setEnabled(false);
			}
			
		}
	};
	
	private View.OnClickListener mBtnOKListener = new View.OnClickListener() {		
		@Override
		public void onClick(View v) 
		{
			String password1 = null;
			String password2 = null;
			SBoxConfig config = null;
			
			config = SBoxConfig.getInstance(SettingActivity.this);
			
			if( mCheckPassword.isChecked() )
			{
				password1 = mEditPassword1.getText().toString();
				password2 = mEditPassword2.getText().toString();
				if( password1.length() >= 4 && password1.equals(password2) )
				{		
					config.mFirstTime = false;
					config.mNeedPassword = true;
					config.mPasswordHash = Utils.enctypt(SettingActivity.this, password1);
					config.saveConfig();
					
					Utils.closeInputMethod(SettingActivity.this, v);
					SettingActivity.this.finish();
				}
				else
				{
					Utils.closeInputMethod(SettingActivity.this, v);
					Toast.makeText(SettingActivity.this, SettingActivity.this.getText(R.string.str_setting_password_invalid), Toast.LENGTH_LONG).show();
					mEditPassword1.setText("");
					mEditPassword2.setText("");
					mEditPassword1.requestFocus();
				}
			}
			else
			{
				Utils.closeInputMethod(SettingActivity.this, v);
				
				if( config.mNeedPassword )
				{
					Dialog dialog = new AlertDialog.Builder(SettingActivity.this).setMessage(R.string.str_setting_password_confirm_clear).setPositiveButton(R.string.str_yes, mConfirmPasswordClearListener).setNegativeButton(R.string.str_cancel, mConfirmPasswordClearListener).create();				
					dialog.show();
				}	
				else
				{
					SettingActivity.this.finish();
				}
			}
		}
	};
	
	private View.OnClickListener mBtnCancelListener = new View.OnClickListener() {		
		@Override
		public void onClick(View v) 
		{
			Utils.closeInputMethod(SettingActivity.this, v);
			SettingActivity.this.finish();
		}
	};
	
	
	private DialogInterface.OnClickListener mConfirmPasswordClearListener = new DialogInterface.OnClickListener() 
	{		
		@Override
		public void onClick(DialogInterface dialog, int which) {
			if( which == DialogInterface.BUTTON_POSITIVE )
			{
				SBoxConfig config = SBoxConfig.getInstance(SettingActivity.this);
				
				config.mNeedPassword = false;
				config.mPasswordHash = "";
				config.saveConfig();				
				
				SettingActivity.this.finish();
			}
			else
			{
			}
		}
	};	
	
	private TitleBar mTitleBar = null;
	private CheckBox mCheckPassword = null;
	private EditText mEditPassword1 = null;
	private EditText mEditPassword2 = null;
	private Button mBtnOK = null;
	private Button mBtnCancel = null;
	
	@Override
	public void onCreate(Bundle savedBundle)
	{
		super.onCreate(savedBundle);
		setContentView(R.layout.setting_view);
		
		mTitleBar = (TitleBar)findViewById(R.id.setting_title);
		mTitleBar.getButton1().setVisibility(View.INVISIBLE);
		mTitleBar.getButton2().setVisibility(View.INVISIBLE);
		
		mCheckPassword = (CheckBox)findViewById(R.id.setting_chk_password);
		mCheckPassword.setOnCheckedChangeListener(mCheckedListener);
		
		mEditPassword1 = (EditText)findViewById(R.id.setting_password1);
		mEditPassword2 = (EditText)findViewById(R.id.setting_password2);
		mBtnOK = (Button)findViewById(R.id.setting_ok);
		mBtnOK.setOnClickListener(mBtnOKListener);
		
		mBtnCancel = (Button)findViewById(R.id.setting_cancel);
		mBtnCancel.setOnClickListener(mBtnCancelListener);
		
		init();
	}
	
	private void init()
	{
		SBoxConfig config = SBoxConfig.getInstance(this);
		if( config.mNeedPassword || config.mFirstTime )
			mCheckPassword.setChecked(true);
		else
			mCheckPassword.setChecked(false);
	}

}
